package com.jgp.workflow.mapper;

import com.jgp.workflow.pojo.ProcApplyState;
import com.jgp.workflow.pojo.ProcVar;
import com.jgp.workflow.pojo.TaskComment;
import com.jgp.workflow.pojo.ViewProcessTask;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Set;

/**
 * 项目   jgp-cloud-parent
 * 作者   loufei
 * 时间   2018/12/10
 */
public interface TaskMapper {
    
    /**
     * 查询待审批任务
     * @param userId
     * @param roleIds
     * @return
     *//*
    @Select({"<script>","SELECT DISTINCT * FROM ( SELECT DISTINCT " ,
                    "T.ID_ AS taskId, " ,
                    "T.CREATE_TIME_ AS createTime, " ,
                    "T.PROC_INST_ID_ AS procId, " ,
                    "T.ASSIGNEE_ AS assignee, " ,
                    "T.OWNER_ AS `owner`, " ,
                    "T.NAME_ AS `name`, " ,
                    "H.START_USER_ID_ AS startUserName, " ,
                    "H.BUSINESS_KEY_ AS businessKey ," ,
                    "D.NAME_ AS procName " ,
                    "FROM ACT_RU_TASK T " ,
                    "INNER JOIN ACT_RE_PROCDEF D ON T.PROC_DEF_ID_ = D.ID_ " ,
                    "INNER JOIN ACT_HI_PROCINST H ON H.PROC_INST_ID_ = T.PROC_INST_ID_ " ,
                    "<when test='userId!=null'>",
                    "WHERE T.ASSIGNEE_ = #{userId} " ,
                    "</when>",
                    "<when test='roleIds!=null'>",
                    "UNION ALL SELECT DISTINCT " ,
                    "T.ID_ AS taskId, " ,
                    "T.CREATE_TIME_ AS createTime, " ,
                    "T.PROC_INST_ID_ AS procId, " ,
                    "T.ASSIGNEE_ AS assignee, " ,
                    "T.OWNER_ AS `owner`, " ,
                    "T.NAME_ AS `name`, " ,
                    "H.START_USER_ID_ AS startUserName, " ,
                    "H.BUSINESS_KEY_ AS businessKey ," ,
                    "D.NAME_ AS procName " ,
                    "FROM ACT_RU_TASK T " ,
                    "INNER JOIN ACT_RE_PROCDEF D ON T.PROC_DEF_ID_ = D.ID_ " ,
                    "INNER JOIN ACT_RU_IDENTITYLINK I ON I.TASK_ID_ = T.ID_ " ,
                    "INNER JOIN ACT_HI_PROCINST H ON H.PROC_INST_ID_ = T.PROC_INST_ID_ " ,
                    "WHERE T.ASSIGNEE_ IS NULL " ,
                    "AND I.TYPE_ = 'candidate' " ,
                    "AND I.GROUP_ID_ IN " ,
                    "<foreach item='item' index='index' collection='roleIds' open='(' separator=', ' close=')'> " ,
                    "#{item}" ,
                    "</foreach>" ,
                    "</when>",
                    ") AS task ORDER BY createTime DESC","</script>"})
    List<ViewProcessTask> queryToDoTask(@Param("userId") String userId, @Param("roleIds")List<String> roleIds);
    
    *//**
     * 查询已审批任务
     * @param userId
     * @return
     *//*
    @Select({"<script>","SELECT DISTINCT * FROM ( SELECT DISTINCT " ,
                    "T.ID_ AS taskId, " ,
                    "T.CREATE_TIME_ AS createTime, " ,
                    "T.PROC_INST_ID_ AS procId, " ,
                    "T.ASSIGNEE_ AS assignee, " ,
                    "T.OWNER_ AS `owner`, " ,
                    "T.NAME_ AS `name`, " ,
                    "H.START_USER_ID_ AS startUserName, " ,
                    "H.BUSINESS_KEY_ AS businessKey ," ,
                    "D.NAME_ AS procName " ,
                    "FROM ACT_RU_TASK T " ,
                    "INNER JOIN ACT_RE_PROCDEF D ON T.PROC_DEF_ID_ = D.ID_ " ,
                    "INNER JOIN ACT_HI_PROCINST H ON H.PROC_INST_ID_ = T.PROC_INST_ID_ " ,
                    "<when test='userId!=null'>",
                    "WHERE T.ASSIGNEE_ = #{userId} " ,
                    "</when>",
                    ") AS task ORDER BY createTime DESC","</script>"})
    List<ViewProcessTask> queryDidTask(@Param("userId") String userId);
    
    *//**
     * 查询已审批任务
     * @param userId
     * @return
     *//*
    @Select({"<script>","SELECT DISTINCT * FROM (" ,
                    "SELECT DISTINCT " ,
                    "H.PROC_INST_ID_ AS id, " ,
                    "H.START_USER_ID_ AS startUserName, " ,
                    "D.NAME_ AS `name`, " ,
                    "H.START_TIME_ AS `createTime`, " ,
                    "H.END_TIME_ AS `completeTime` " ,
                    "FROM ACT_HI_PROCINST H " ,
                    "INNER JOIN ACT_RE_PROCDEF D ON D.ID_ = H.PROC_DEF_ID_ " ,
                    "INNER JOIN ACT_HI_TASKINST T ON T.PROC_INST_ID_ = H.PROC_INST_ID_ " ,
                    "WHERE T.ASSIGNEE_ = #{userId} " ,
                    "UNION ALL SELECT DISTINCT " ,
                    "H.PROC_INST_ID_ AS id, " ,
                    "H.START_USER_ID_ AS startUserName, " ,
                    "D.NAME_ AS `name`, " ,
                    "H.START_TIME_ AS `createTime`, " ,
                    "H.END_TIME_ AS `completeTime` " ,
                    "FROM ACT_HI_PROCINST H " ,
                    "INNER JOIN ACT_RE_PROCDEF D ON D.ID_ = H.PROC_DEF_ID_ " ,
                    "INNER JOIN ACT_HI_TASKINST T ON T.PROC_INST_ID_ = H.PROC_INST_ID_ ",
                    "INNER JOIN ACT_HI_IDENTITYLINK I ON I.PROC_INST_ID_ = H.PROC_INST_ID_ " ,
                    "WHERE I.USER_ID_ = #{userId} ",
                    "AND I.TYPE_ = 'participant' " ,
                    ") AS process " , "</script>"})
    List<ViewProcessIns> queryApprovedTask(@Param("userId") String userId);*/
    
    
    @Select({"<script>","SELECT PROC_INST_ID_ AS procId," ,
                    "IF (COUNT(*) = 1, 'true', 'false') AS applyNode " ,
                    "FROM " ,
                    "ACT_HI_TASKINST " ,
                    "WHERE " ,
                    "PROC_INST_ID_ IN" ,
                    "<foreach collection='procIds' item='id' open='(' separator=',' close=')'>",
                    "#{id}",
                    "</foreach>",
                    "GROUP BY PROC_INST_ID_" , "</script>"})
    List<ProcApplyState> queryProcApplyState(@Param("procIds") List<String> procIds);
    
    
    /**
     * 查询已经提交单未传递的工单数量
     *
     * @param userId
     * @return
     */
    @Select({"<script>","SELECT" ,
            "count(t.ID_)" ,
            "FROM ACT_HI_PROCINST p,ACT_HI_TASKINST t" ,
            "WHERE p.ID_ IN (" ,
            "SELECT ht.PROC_INST_ID_ FROM ACT_HI_TASKINST ht WHERE ht.PROC_INST_ID_ IN ( SELECT rul.PROC_INST_ID_ FROM ACT_RU_IDENTITYLINK rul WHERE rul.USER_ID_ = #{userId} ) GROUP BY ht.PROC_INST_ID_ HAVING count(ht.ID_) = 1 )" ,
            "AND t.PROC_INST_ID_ = p.ID_", "</script>"})
    Long countApplyButNotPass(@Param("userId") String userId);
    
    /**
     * 查询已经提交单未传递的工单
     *
     * @param userId
     * @param pageStart 分页起始下标
     * @param pageOffset 每页条数
     * @return
     */
    @Select({"<script>","SELECT" ,
            "t.ID_ taskId,t.OWNER_ OWNER,t.PROC_INST_ID_ procId,t.ASSIGNEE_ assignee,t.FORM_KEY_ formKey,t.START_TIME_ createTime,t.NAME_ name,p.BUSINESS_KEY_ businessKey,p.START_USER_ID_ startUser,p.NAME_ procName,t.PRIORITY_ priority" ,
            "FROM ACT_HI_PROCINST p,ACT_HI_TASKINST t" ,
            "WHERE p.ID_ IN (" ,
            "SELECT ht.PROC_INST_ID_ FROM ACT_HI_TASKINST ht WHERE ht.PROC_INST_ID_ IN ( SELECT rul.PROC_INST_ID_ FROM ACT_RU_IDENTITYLINK rul WHERE rul.USER_ID_ = #{userId} ) GROUP BY ht.PROC_INST_ID_ HAVING count(ht.ID_) = 1 )" ,
            "AND t.PROC_INST_ID_ = p.ID_ ORDER BY p.START_TIME_ DESC limit #{pageStart},#{pageOffset}", "</script>"})
    List<ViewProcessTask> queryApplyButNotPass(@Param("userId") String userId,@Param("pageStart") int pageStart,@Param("pageOffset") int pageOffset);
    
    
    @Select({"<script>","SELECT TASK_ID_ `taskId`,MESSAGE_ `comment`, TIME_ `time`" ,
            "FROM ACT_HI_COMMENT" ,
            "WHERE TASK_ID_ IN" ,
            "<foreach collection='taskIds' item='id' open='(' separator=',' close=')'>",
            "#{id}",
            "</foreach>",
            " AND TYPE_='comment' ",
            " ORDER BY  TIME_ DESC ",
            "</script>"})
    List<TaskComment> queryTaskComments(@Param("taskIds") List<String> taskIds);
    
    
    @Select({"<script>","SELECT TASK_ID_ `taskId`,MESSAGE_ `comment`, TIME_ `time`" ,
            "FROM ACT_HI_COMMENT" ,
            "WHERE PROC_INST_ID_ = #{procId}" ,
            "AND TASK_ID_ = #{taskId}" ,
            "</script>"})
    List<TaskComment> queryTaskComments2(@Param("procId") String procId,@Param("taskId") String taskId);
    
    @Select({"<script>","SELECT COUNT(DISTINCT PROC_INST_ID_) FROM ACT_HI_PROCINST","</script>"})
    Long queryAllProcCount();
    
    @Select({"<script>","SELECT PROC_INST_ID_ `procId`,NAME_ `name`, VAR_TYPE_ `type`, LONG_ `longValue`, DOUBLE_ `doubleValue`, TEXT_ `textValue` " ,
            "FROM act_hi_varinst" ,
            "WHERE PROC_INST_ID_ IN" ,
            "<foreach collection='procIds' item='id' open='(' separator=',' close=')'>",
            "#{id}",
            "</foreach>",
            "<if test='name != null'>",
            " AND NAME_= #{name}",
            "</if>",
            "</script>"})
    List<ProcVar> getProcVar(@Param("procIds") Set<String> procIds,@Param("name") String name);
    
    
}
